import { defineStore } from "pinia";
import { UserState } from "./types/type.ts";
import { LoginFormData } from "@/api/user/types.ts";
import { reqLogin } from "@/api/user";
import { SET_TOKEN, GET_TOKEN } from "@/util/token";
import { constantRoute } from "@/router/routes.ts";

let useUserStore = defineStore("User", {
  state: (): UserState => {
    return {
      token: GET_TOKEN(),
      menuList: constantRoute,
    };
  },
  actions: {
    // 用户登录逻辑
    async userLogin(data: LoginFormData) {
      let result: any = await reqLogin(data);
      if (result.code == 200) {
        this.token = result.token as string;
        SET_TOKEN(result.token as string);
        return "ok";
      } else {
        return Promise.reject(new Error(result.message));
      }
    },
  },
});

export default useUserStore;
